﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WeifenLuo.WinFormsUI.Base
{
    public partial class FormBaseDataMstDtl : FormBaseData
    {
        private bool mb_GridChanged = false;
        protected int mi_NumOfDetailRows = 0;
        protected CurrencyManager mRowManagerDetail;

        public FormBaseDataMstDtl()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            SetCurrentDataGrid(mdbg_DetailGrid);
            base.BasicForm_Load(sender, e);
        }

        protected override void BaseForm_Activated(object sender, EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
            if (ms_currentField != null && ms_valueCurrentField != null)
                mMainForm.stt_Bar.Text = ms_currentField + ": " + ms_valueCurrentField;
        }

        protected override void BasicForm_Closing(object sender, FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
            mRowManagerDetail = null;
            if (mb_GridChanged) mb_GridChanged = false;
        }

        // For using set the currentField and the value current field
        public override void BaseDataForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (sender.Equals(mdbg_DetailGrid))
                // get current column when user click header.
                mi_currentCol = mdbg_DetailGrid.HitTest(e.X, e.Y).ColumnIndex;
        }

        public override void RowMaster_Changed(object sender, EventArgs e)
        {
            if (GetCurrentAction() == MODES.ADD) return;
            base.RowMaster_Changed(sender, e);
            LoadDetailData();
        }

        protected override void LoadMasterData()
        {
            base.LoadMasterData();
            LoadDetailData();
        }

        protected virtual void LoadDetailData()
        {	// Call the base at the end of the method
            if (mRowManagerDetail != null)
            {
                mRowManagerDetail.PositionChanged += new EventHandler(RowDetail_Changed);
                mRowManagerDetail.ItemChanged += new ItemChangedEventHandler(ItemDetail_Changed);
            }
        }

        public override void modify()
        {
            base.modify();
            //mdbg_DetailGrid.AllowUpdate = true;
            //mdbg_DetailGrid.AllowSort = false;
            mdbg_DetailGrid.AllowUserToAddRows = true;
            mdbg_DetailGrid.AllowUserToDeleteRows = false;
            mdbg_DetailGrid.AllowUserToOrderColumns = false;
        }

        public override void add()
        {
            base.add();
            //mdbg_DetailGrid.AllowUpdate = true;
            //mdbg_DetailGrid.AllowSort = false;
            mdbg_DetailGrid.AllowUserToAddRows = true;
            mdbg_DetailGrid.AllowUserToDeleteRows = false;
            mdbg_DetailGrid.AllowUserToOrderColumns = false;
        }

        public override void save()
        {
            //mdbg_DetailGrid.AllowUpdate = false;
            //mdbg_DetailGrid.AllowSort = true;
            mdbg_DetailGrid.AllowUserToAddRows = false;
            mdbg_DetailGrid.AllowUserToDeleteRows = true;
            mdbg_DetailGrid.AllowUserToOrderColumns = true;
            base.save();
        }

        public override void post_delete()
        {
            base.post_delete();
        }

        public override void cancel()
        {
            //mdbg_DetailGrid.AllowUpdate = false;
            //mdbg_DetailGrid.AllowSort = true;
            mdbg_DetailGrid.AllowUserToAddRows = false;
            mdbg_DetailGrid.AllowUserToDeleteRows = true;
            mdbg_DetailGrid.AllowUserToOrderColumns = true;
            base.cancel();
            LoadDetailData();
        }

        public override void full_mode()
        {
            //mdbg_DetailGrid.AllowUpdate = false;
            //mdbg_DetailGrid.AllowSort = true;
            mdbg_DetailGrid.AllowUserToAddRows = false;
            mdbg_DetailGrid.AllowUserToDeleteRows = true;
            mdbg_DetailGrid.AllowUserToOrderColumns = true;
            base.full_mode();
        }

        public override void void_mode()
        {
            //mdbg_DetailGrid.AllowUpdate = false;
            mdbg_DetailGrid.AllowUserToAddRows = false;
            mdbg_DetailGrid.AllowUserToDeleteRows = false;
            mdbg_DetailGrid.AllowUserToOrderColumns = true;
            base.void_mode();
        }

        //private void mdbg_DetailGrid_RowColChange(object sender, C1.Win.C1TrueDBGrid.RowColChangeEventArgs e)
        //{
        //    try
        //    {
        //        if (mMainForm != null && mdbg_DetailGrid.DataSource != null)
        //        {
        //            ms_currentField = mdbg_DetailGrid.Splits[0].DisplayColumns[mdbg_DetailGrid.Col].Name;
        //            ms_valueCurrentField = mdbg_DetailGrid.Splits[0].DisplayColumns[mdbg_DetailGrid.Col].DataColumn.Value.ToString();
        //            mMainForm.stbUnderMouse.Text = ms_currentField + ": " + ms_valueCurrentField;
        //            ms_currentField = mdbg_DetailGrid.Splits[0].DisplayColumns[mdbg_DetailGrid.Col].Name;
        //            ms_valueCurrentField = mdbg_DetailGrid.Splits[0].DisplayColumns[mdbg_DetailGrid.Col].DataColumn.Value.ToString();
        //            mMainForm.stbUnderMouse.Text = ms_currentField + ": " + ms_valueCurrentField;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Ctrl_LOG.Print("BaseDataGridForm", "In event RowCol Change.", "Error message: '" + ex.Message + "'");
        //    }
        //}

        private void mdbg_DetailGrid_KeyUp(object sender, KeyEventArgs e)
        {	// Define the ShortCut key for openning the selection form
            if (GetCurrentAction() == MODES.FULL ||
               GetCurrentAction() == MODES.VOID) return;
            if (e.KeyCode != Keys.F9) return;
            OpenSelection(mdbg_DetailGrid.Columns[mdbg_DetailGrid.CurrentCell.ColumnIndex].Name);
        }

        //private void mdbg_DetailGrid_ColMove(object sender, C1.Win.C1TrueDBGrid.ColMoveEventArgs e)
        //{
        //    mb_GridChanged = true;
        //}

        //private void mdbg_DetailGrid_ColResize(object sender, C1.Win.C1TrueDBGrid.ColResizeEventArgs e)
        //{
        //    mb_GridChanged = true;
        //}

        private void mdbg_DetailGrid_Leave(object sender, EventArgs e)
        {
            if (mb_GridChanged) mb_GridChanged = false;
        }

        //private void mdbg_DetailGrid_Error(object sender, ErrorEventArgs e)
        //{
        //}
        // This event is used for Detail Grid
        public virtual void RowDetail_Changed(object sender, EventArgs e)
        {
        }
        // This event is used for Detail Grid
        public virtual void ItemDetail_Changed(object sender, ItemChangedEventArgs e)
        {
        }

        protected virtual void mdbg_DGV_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left) return;
            if (GetCurrentAction() != MODES.ADD && GetCurrentAction() != MODES.MODIFY)
                mnu_Delete.Enabled = false;
            else
            {
                ((DataGridView)sender).Focus();
                mnu_Delete.Enabled = true;
            }
        }

        private void mnu_Delete_Click(object sender, EventArgs e)
        {
            DeleteRowGrid();
        }
    }
}
